import { Router } from 'express';
import Payload from '../models/Payload';
var models = require('../sqlModels');
let Page = require('../models/Page');
export default () => {
    let api = Router();

    // savingInfo新增测试
    api.post('/create', async (req, res, next) => {
        let savingInfoData = req.body;
        try {
            let result = await models.savingInfo.create({
                accountname: savingInfoData.accountname,
                savingbank: savingInfoData.savingbank,
                accounttype: savingInfoData.accounttype,
                moneytype: savingInfoData.moneytype,
                operationtype: savingInfoData.operationtype,
                savingcount: savingInfoData.savingcount,
                savingdec: savingInfoData.savingdec,
                usetime: savingInfoData.usetime
            })
            res.status(200).json(new Payload(result));
        } catch (error) {
            next(error);
        }
    });

    //获取全部savingInfo,支持分页
    api.get('/all', async (req, res, next) => {
        try {
            let condition = {
                raw: true
            };
            if (req.query.currentPage && req.query.numPerPage) {
                var numPerPage = parseInt(req.query.numPerPage);
                var currentPage = parseInt(req.query.currentPage);
                condition.offset = numPerPage * (currentPage - 1);
                condition.limit = numPerPage;
            }

            let result = await models.savingInfo.findAndCountAll(condition);
            let resultLength = result.rows.length;
            let page = new Page(result.rows, currentPage || 1,
                resultLength, numPerPage || resultLength, result.count);
            res.json(new Payload(page));

        } catch (error) {
            next(error);
        }
    })


   //更新savingInfo
    api.put('/:savingInfoId', async(req, res, next) => {
        let savingInfoId = req.params.savingInfoId;
        let savingInfoData = req.body;
       try{
           let result = await models.savingInfo.update({  
                accountname: savingInfoData.accountname,
                savingbank: savingInfoData.savingbank,
                accounttype: savingInfoData.accounttype,
                moneytype: savingInfoData.moneytype,
                operationtype: savingInfoData.operationtype,
                savingcount: savingInfoData.savingcount,
                savingdec: savingInfoData.savingdec,
                usetime: savingInfoData.usetime
            }, {
            where: {
                id: savingInfoId
            }
        })
        res.status(200).json(new Payload(result));
       }catch(error){
           next(error);
       }

    })



//根据ID删除
api.delete('/:savingInfoId', async(req, res, next) => {
        let savingInfoId = req.params.savingInfoId;
       try{
           let result = await models.savingInfo.destroy({   
                 where: {
                id: savingInfoId
            }
        })
        res.status(200).json(new Payload(result));
       }catch(error){
           next(error);
       }

    })


    return api;
}
